<?php
namespace app\api\controller;

use app\common\controller\Api;
use think\Request;
use app\admin\model\Sign;

/**
 * 用户签到
 */
class Signcon extends Api {
	
	// 无需登录的接口,*表示全部
	protected $noNeedLogin = ['*'];
	// 无需鉴权的接口,*表示全部
	protected $noNeedRight = ['*'];
	
	/**
	 * 查询用户签到页面信息
	 *
	 * @ApiMethod   (POST)
	 * @ApiRoute    (/api/Sign/getSignInfo/uid/{uid})
	 * @ApiParams   (name="uid", type="integer", required=true, description="用户ID")
	 */
	public function getSignInfo() {
		$uid = $this->auth->id;
		$appid = $this->auth->app_id;
		if (!empty($uid) && !empty($appid)) {
			// 查询10天内的签到记录
			$where['user_id'] = $uid;
			$where['app_id'] = $appid;
			$tenbefore = date("Y-m-d H:i:s",strtotime("-10 day"));
			$sl = Sign::where($where)->whereTime('sign_day', '>=', $tenbefore)->order('sign_day','desc')->select();
			$today = date("Y-m-d 00:00:00",time());// 今天
			$isTodaySign = false;
			$signMax = 0;
			$size = count($sl);
			$signDayList = array();
			if($size > 0) {
				for($i=0; $i<$size; $i++) {
					if($i == 0) {
						$signMax = $sl[$i]['max_sign'];
						if($today == $sl[$i]['sign_day']) {
							$isTodaySign = true;
						}
					}
					if($i < $signMax) {
						$signDayList[] = $sl[$i]['sign_day'];
					}
				}
			}
			$signDayList = array_reverse($signDayList);
			$date = [
				'isTodaySign'=>$isTodaySign,
				'signMax'=>$signMax,
				'signDayList'=>$signDayList
			];
			$this->success('返回成功', $date);
		} else {
			$this->error(__('Invalid parameters'));
		}
	}
	
	/**
	 * 用户签到
	 *
	 * @ApiMethod   (POST)
	 * @ApiRoute    (/api/Sign/signIn/uid/{uid})
	 * @ApiParams   (name="uid", type="integer", required=true, description="用户ID")
	 */
	public function signIn() {
		$uid = $this->auth->id;
		$appid = $this->auth->app_id;
		$type = $this->request->request('type');
		$ip = Request::instance()->ip(0, false);
		if (!empty($uid) && !empty($appid) && (is_numeric($type) === true) && !empty($ip)) {
			// 查询10天内的签到记录
			$where['user_id'] = $uid;
			$where['app_id'] = $appid;
			$tenbefore = date("Y-m-d H:i:s",strtotime("-10 day"));
			$sl = Sign::where($where)->whereTime('sign_day', '>=', $tenbefore)->order('sign_day','desc')->select();
			$today = date("Y-m-d 00:00:00",time());// 今天
			$isTodaySign = false;
			$signMax = 0;
			$size = count($sl);
			$signDayList = array();
			if($size > 0) {
				for($i=0; $i<$size; $i++) {
					if($i == 0) {
						$signMax = $sl[$i]['max_sign'];
						if($today == $sl[$i]['sign_day']) {
							$isTodaySign = true;
						}
					}
					if($i < $signMax) {
						$signDayList[] = $sl[$i]['sign_day'];
					}
				}
			}
			if($isTodaySign) {
				$this->success('重复签到');
				die;
			} else {
				// 添加签到记录
				$dat = [
				    'user_id'           => $uid,
					'app_id'         	=> $appid,
				    'ip'        		=> $ip,
					'sign_day'			=> date("Y-m-d",time()),
					'type'        		=> $type,
					'max_sign'        	=> $signMax+1,
				];
				Sign::insert($dat);
				$signDayList = array_reverse($signDayList);
				$signDayList[] = $today;// 加入今天的记录
				// 发放奖励
				
				// 返回结果
				$date = [
					'isTodaySign'=>true,
					'signMax'=>$signMax+1,
					'signDayList'=>$signDayList
				];
				$this->success('返回成功', $date);
			}
		} else {
			$this->error(__('Invalid parameters'));
		}
	}
}